/**
 * Filename: DraweePagerAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/13 16:41
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.view.FrePhotoView;
import com.liulishuo.qiniuimageloader.QiniuImageLoader;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 大图浏览适配器
 */
public class DraweePagerAdapterhx extends PagerAdapter{

	private Activity 		mContext;
	private List<String>  	mUrls;
	private ArrayList<View> mViews;
	private ImageLoader 	mImageLoader;
	private Boolean 		isLocal=false;

	public DraweePagerAdapterhx(Activity mContext, List<String> mUrls , Boolean isLocal) {
		this.mContext = mContext;
		this.mUrls = mUrls;
		this.mImageLoader = ImageLoader.getInstance();
		this.isLocal = isLocal;
	}


	@Override
	public int getCount() {
		return mUrls.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public View instantiateItem(ViewGroup container, int position) {
		String url = mUrls.get(position);
		url = isLocal?"file://" + Uri.parse(url) : url ;
		final FrePhotoView photoDraweeView = new FrePhotoView(mContext);
		photoDraweeView.setImageUri(url,null,mContext);
		try {
			container.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return photoDraweeView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager)container).removeView((View) object);
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public String getPic(int position) {
		return mUrls.size() > 0 ? mUrls.get(position % mUrls.size()) : "";
	}

	public Bitmap getBitmap(int position) {
		Bitmap bitmap = null;
		FrePhotoView view = (FrePhotoView) mViews.get(position % mViews.size());
		Drawable drawable = view.getDrawable();
		if(drawable != null && drawable instanceof BitmapDrawable) {
			BitmapDrawable bd = (BitmapDrawable) drawable;
			bitmap = bd.getBitmap();
		}
		return bitmap;
	}
}
